from itsdangerous import BadSignature
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
#
from mall import settings


def generic_verify_url(user_id,email):

    """
    创建序列化器
    组织数据
    加密
    返回数据
    :param user_id:
    :param email:
    :return:
    """
    # 创建序列化器
    serializer = Serializer(settings.SECRET_KEY,3600)
    # 组织数据
    data = {
        'id':user_id,
        'email':email
    }
    # 加密
    token = serializer.dumps(data)  #byte类型
    # 返回数据
    return 'http://www.meiduo.site:8080/success_verify_email.html?token=' + token.decode()


def check_active_token(token):
    serializer = Serializer(settings.SECRET_KEY,3600)
    try:
        result = serializer.loads(token)
    except BadSignature:
        return None
    else:
        # return result.get('id')
        return result
